#include "stdafx.h"
#include "CppUnitTest.h"

#include "System/Base/ExceedMemoryException.h"
#include "System/Base/Scene.h"
#include "System/Base/Case.h"


using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Native::System;

namespace TestNativeSystem
{
    TEST_CLASS(ExceedMemoryException_Tester)
    {
    public:
        wstring _class;
        size_t _line;
        wstring _detail;
        wstring _arguments;
        size_t _checked_size;
        size_t _allowable_size;

        ExceedMemoryException_Tester()
        {
            _class = L"ExceedMemoryException_Tester";
            _detail = L"Test exception detail";
            _arguments = L"location";
            _line = 189;
            _checked_size = 45u;
            _allowable_size = 16u;
        }

        TEST_METHOD(ExceedMemoryException_Throw)
        {
            CScene location(_class, L"ExceedMemoryException_Throw", _line);
            wstring typeName;
            wchar_t message[CException::MAX_MESSAGE_LENGTH] = {0};

            wsprintf(message, L"Input size:[%u] exceeds allowable size:[%u]", _checked_size, _allowable_size);

            try {
                CExceedMemoryException exception(location, 45u, 16u, EMPTY_STRING, _detail);
                typeName = exception.TypeName();

                throw exception;
            } catch(CException& e) {
                Assert::AreEqual(e.TypeName(), typeName);

                auto record = e.LatestRecord();

                Assert::AreEqual(record.Scene().Class(), _class);
                Assert::AreEqual(record.Scene().Method().c_str(), L"ExceedMemoryException_Throw");
                Assert::AreEqual(record.Scene().Line(), 189u);
                Assert::AreEqual(record.Message().c_str(), message);
                Assert::AreEqual(record.Arguments(), EMPTY_STRING);
                Assert::AreEqual(record.Detail(), _detail);
            }
        }

        TEST_METHOD(ExceedMemoryException_CheckWithoutArgument)
        {
            CScene location(_class, L"ExceedMemoryException_CheckWithoutArgument", _line);
            wchar_t message[CException::MAX_MESSAGE_LENGTH] = {0};

            wsprintf(message, L"Input size:[%u] exceeds allowable size:[%u]", _checked_size, _allowable_size);

            try {
                CExceedMemoryException::Check(location, _checked_size, _allowable_size, _arguments, _detail);
            } catch(CException& e) {
                auto record = e.LatestRecord();

                Assert::AreEqual(record.Scene().Class(), _class);
                Assert::AreEqual(record.Scene().Method().c_str(), L"ExceedMemoryException_CheckWithoutArgument");
                Assert::AreEqual(record.Scene().Line(), 189u);
                Assert::AreEqual(record.Message().c_str(), message);
                Assert::AreEqual(record.Arguments(), _arguments);
                Assert::AreEqual(record.Detail(), _detail);
            }
        }
    };
}